<?php 
class CartWS_Model_SalesLineProperty extends CartWS_Model_FormulaEngine_Table
{
	protected $_name = 'sc_sales_line_property';
	protected $_sequence = 'sc_sales_line_property_seq';
	protected $_referenceMap = array(
        'SalesOrderLine' => array(
            'columns'           => array( 'sales_line_id' ),
            'refTableClass'     => 'Default_Model_SalesOrderLine',
            'refColumns'        => array( 'sales_line_id' )
        ),
        'CartWS_Model_SalesOrderLine' => array(
            'columns'           => 'sales_line_id',
            'refTableClass'     => 'CartWS_Model_SalesOrderLine',
            'refColumns'        => 'sales_line_id'
        ),

    );
    
    public function getLineProperty( $lineId)
    {
    	$rowset = $this->fetchAll( 
    		$this->select()->from('sc_sales_line_property')
    		->columns('property')
    		->columns('value')
    		->where( 'sales_line_id = ?', $lineId )
    	);
    	if( count( $rowset )) {
    		return $rowset->toArray();
    	}
    	return array();
    }
    
    public function deleteFromLine( $lineId )
    {
        // @todo: Borrar las propiedades.
    	$this->delete( 'sales_line_id = ' . $lineId );
    }
    
    public function addProperties ( $lineId, $properties )
    {
		foreach($properties as $key=>$value ) {
			$this->addProperty( $lineId, $key, $value);
		}
    
    }
    
    public function addProperty( $lineId, $propery, $value)
    {
		$row = $this->fetchRow(
			$this->select()
			->where('sales_line_id =?', $lineId)
			->where('property=?', $propery)
		);
		if( !count($row )) {
			$row = $this->createRow();
			$row->sales_line_id = $lineId;
			$row->property = $propery;
		}
		$row->value = $value;
		$row->save();
    }
}